package pl.squeaky.mvc.wizard;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.dltk.core.IModelElement;
import org.eclipse.dltk.core.ModelException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import pl.squeaky.mvc.generator.ControllerGenerator;
import pl.squeaky.mvc.generator.ViewGenerator;
import pl.squeaky.mvc.nature.MvcNature;

public class NewControllerWizard extends Wizard implements INewWizard {

	private IStructuredSelection selection;

	public NewControllerWizard() {
		setWindowTitle("New controller");
	}

	@Override
	public boolean performFinish() {
		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			Object element = structuredSelection.getFirstElement();
			
			IProject project = null;
			if(element instanceof IResource) {
				IResource resource = (IResource)element;
				
				project = resource.getProject();
			} else if(element instanceof IModelElement) {
				IModelElement modelElement = (IModelElement)element;
				
				try {
					project = modelElement.getUnderlyingResource().getProject();
				} catch (ModelException e) {
					throw new RuntimeException(e);
				}
			}
			MvcNature nature;
			try {
				nature = (MvcNature)project.getNature(MvcNature.NATURE_ID);
			} catch (CoreException e) {
				throw new RuntimeException(e);
			}
			
			NewControllerNamePage namePage = (NewControllerNamePage)getPage("NewControllerNamePage");
			
			ControllerGenerator generator = new ControllerGenerator(namePage.getControllerName());
			generator.setSuperclass(namePage.getSuperclassName());
			
			NewControllerActionsPage actionsPage = (NewControllerActionsPage)getPage("NewControllerActionsPage");
			
			if(actionsPage.getInit()) {
				generator.addInitHook();
			}
			if(actionsPage.getPreDispatch()) {
				generator.addPreDispatchHook();
			}
			if(actionsPage.getPostDispatch()) {
				generator.addPostDispatchHook();
			}
			
			if(actionsPage.getGenerateViews()) {
				ViewGenerator.setupViewsFolderStructure(nature.getViews().getPath(), namePage.getControllerName());
			}
			
			for(String action : actionsPage.getActions()) {
				generator.addAction(action);
				
				if(actionsPage.getGenerateViews()) {
					ViewGenerator viewGenerator = new ViewGenerator(action);
					viewGenerator.generate(nature.getViews().getPath(), namePage.getControllerName());
				}
			}
			
			IFile file = generator.generate(nature.getControllers().getPath());
			
			IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			
			try {
				IDE.openEditor(page, file, true);
			} catch (PartInitException e) {
				throw new RuntimeException(e);
			}
		}
		
		return true;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
	
	@Override
	public void addPages() {
		addPage(new NewControllerNamePage());
		addPage(new NewControllerActionsPage());
	}
}
